import {
  translateDateString
} from "../Tools"

function roomMsgParser(msgData) {
  let action = "离开";
  if (msgData.isJoin) action = "进入";
  return `<span class="text-gray">[ ${translateDateString(msgData.time)} ]</span> <span class="text-blue">${msgData.user}</span> <span class="text-orange">${action}</span>了房间`;
}

function addCubeByPositionAndSize(msgData, x, y, z, a, b, h) {
  // 验证参数是否合法
  if (arguments.length !== 7 && arguments.length !== 4) return null;
  if (arguments.length === 4) {
    return `<span class="text-gray">[ ${msgData.time} ]</span> <span class="text-blue">${msgData.user}</span> 在 <span class="text-orange">(${x},${y},${z})</span> 位置添加了单位正方体`;
  }
  return `<span class="text-gray">[ ${msgData.time} ]</span> <span class="text-blue">${msgData.user}</span> 在 <span class="text-orange">(${x},${y},${z})</span> 位置添加了长/宽/高为 <span class="text-orange">${a}/${b}/${h}</span> 的立方体`;
}

function addBallByPositionAndSize(msgData, x, y, z, r) {
  // 验证参数是否合法
  if (arguments.length !== 5) return null;
  return `<span class="text-gray">[ ${msgData.time} ]</span> <span class="text-blue">${msgData.user}</span> 添加了以点 <span class="text-orange">(${x},${y},${z})</span> 为球心、以 <span class="text-orange">${r}</span> 为半径的球体`;
}

function addCylinderByPositionAndSize(msgData, x, y, z, r, h) {
  // 验证参数是否合法
  if (arguments.length !== 6) return null;
  const face = Number(h) > 0 ? "底面" : "顶面";
  return `<span class="text-gray">[ ${msgData.time} ]</span> <span class="text-blue">${msgData.user}</span> 以点 <span class="text-orange">(${x},${y},${z})</span> 为${face}圆心，以 <span class="text-orange">${r}</span> 为${face}半径，添加了高为 <span class="text-orange">${Math.abs(h)}</span> 的圆柱体`;
}

function translateGeometryByIdAndOffset(msgData, id, x, y, z) {
  // 验证参数是否合法
  if (arguments.length !== 5) return null;
  return `<span class="text-gray">[ ${msgData.time} ]</span> <span class="text-blue">${msgData.user}</span> 平移了ID为 <span class="text-orange">${id}</span> 的几何体，在x/y/z轴上的平移量分别为 <span class="text-orange">${x}/${y}/${z}</span>`;
}

function rotateGeometryByIdAndAngle(msgData, id, a, b, c) {
  // 验证参数是否合法
  if (arguments.length !== 5) return null;
  return `<span class="text-gray">[ ${msgData.time} ]</span> <span class="text-blue">${msgData.user}</span> 旋转了ID为 <span class="text-orange">${id}</span> 的几何体，沿x/y/z轴旋转的角度分别为 <span class="text-orange">${a}/${b}/${c}</span>`;
}

function colorGeometryByIdAndColor(msgData, id, type, color) {
  // 验证参数是否合法
  if (arguments.length !== 4) return null;
  type = type === "face" ? "表面" : "线条";
  return `<span class="text-gray">[ ${msgData.time} ]</span> <span class="text-blue">${msgData.user}</span> 将ID为 <span class="text-orange">${id}</span> 的几何体的${type}颜色修改为了 <span style="color: ${color};">${color}</span>`;
}

function scaleGeometryByIdAndScale(msgData, id, length, width, height) {
  // 验证参数是否合法
  if (arguments.length !== 5) return null;
  return `<span class="text-gray">[ ${msgData.time} ]</span> <span class="text-blue">${msgData.user}</span> 缩放了ID为 <span class="text-orange">${id}</span> 的几何体，在x/y/z轴方向上缩放的比例为 <span class="text-orange">${length}/${width}/${height}</span>`;
}

function deleteGeometryById(msgData, id) {
  // 验证参数是否合法
  if (arguments.length !== 2) return null;
  return `<span class="text-gray">[ ${msgData.time} ]</span> <span class="text-blue">${msgData.user}</span> 删除了ID为 <span class="text-orange">${id}</span> 的几何体`;
}

const handlers = {
  room: roomMsgParser,
  command: {
    add: {
      cube: [addCubeByPositionAndSize],
      ball: [addBallByPositionAndSize],
      cylinder: [addCylinderByPositionAndSize],
    },
    alter: {
      translation: [translateGeometryByIdAndOffset],
      rotate: [rotateGeometryByIdAndAngle],
      color: [colorGeometryByIdAndColor],
      scale: [scaleGeometryByIdAndScale],
    },
    delete: {
      object: [deleteGeometryById],
    },
  }
};

export default handlers;